package com.zh.test13JDBC;

import java.sql.*;

/**
 * 使用JDBC连接MySQL数据库
 *
 * @author pan_junbiao
 */


public class Test2 {

    public static void main(String[] args)
    {
        try
        {
            // 加载数据库驱动类
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("数据库驱动加载成功");

            // 获取数据库连接对象
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/zh01?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf8&useSSL=false",
                    "root", "1234");
            System.out.println("数据库连接成功");
            //创建会话：
            Statement sta =conn.createStatement();
            //发送SQL：
             ResultSet rs = sta.executeQuery("select * from t_book");
             while(rs.next()){
                 int id = rs.getInt("id");
                 System.out.println(rs.getInt("id")+" "+rs.getString("name") +" "+rs.getString("author") +" "+rs.getString("price")); ;
             }

            //关闭资源
            sta.close();
            conn.close();


        } catch (ClassNotFoundException cnfe)
        {
            cnfe.printStackTrace();
        } catch (SQLException sqle)
        {
            sqle.printStackTrace();
        }


    }

}
